package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 获得运营统计数据
     * Map数据格式：
     *      reportDate（当前时间）--String
     *      todayNewMember（今日新增会员数） -> number
     *      totalMember（总会员数） -> number
     *      thisWeekNewMember（本周新增会员数） -> number
     *      thisMonthNewMember（本月新增会员数） -> number
     *      todayOrderNumber（今日预约数） -> number
     *      todayVisitsNumber（今日到诊数） -> number
     *      thisWeekOrderNumber（本周预约数） -> number
     *      thisWeekVisitsNumber（本周到诊数） -> number
     *      thisMonthOrderNumber（本月预约数） -> number
     *      thisMonthVisitsNumber（本月到诊数） -> number
     *      hotSetmeal（热门套餐（取前4）） -> List<Map<String,Object>>
     */

    /**
     * 运营数据统计
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {

        //进行日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //获取当前时间
        Date today = new Date();

        //格式化
        String todayStr = sdf.format(today);

        //reportDate（当前时间）--String
        String reportDate = todayStr;

        //获取周一
        String moday = sdf.format(DateUtils.getThisWeekMonday());

        //获取周日
        String sunday = sdf.format(DateUtils.getSundayOfThisWeek());

        //获取本月第一天
        String firstDay = sdf.format(DateUtils.getFirstDay4ThisMonth());

        //获取本月最后一天
        String lastDay = sdf.format(DateUtils.getLastDayOfThisMonth());

        //统计会员数量

        //todayNewMember(今日新会员，注册时间不得早于当前时间)
        Integer todayNewMember = memberDao.findMemberCountByDate(todayStr);

        //totalMember(会员总数)
        Integer totalMember = memberDao.findMemberTotalCount();

        //thisWeekNewMember(本周新会员，注册时间不得早于本周一)
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(moday);

        //thisMonthNewMember(本月新会员，注册时间不得早于本月1号)
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay);

        //预约数量统计

        //todayOrderNumber(今日预约数)
        Integer todayOrderNumber = orderDao.findOrderCountByDate(todayStr);

        //todayVisitsNumber(今日到诊数)
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(todayStr);

        //thisWeekOrderNumber(本周预约数)
        Integer thisWeekOrderNumber = orderDao.findOrderCountBetweenDate(moday,sunday);

        //thisWeekVisitsNumber(本周到诊数)
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountByDate(moday);

        //thisMonthOrderNumber(本月预约数)
        Integer thisMonthOrderNumber = orderDao.findOrderCountBetweenDate(firstDay, lastDay);

        //thisMonthVisitsNumber(本月到诊数)
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay);

        //热门套餐(hotSetmeal)
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        //新建map集合
        Map<String, Object> reportData = new HashMap<String, Object>(12);

        //封装数据进map
        reportData.put("reportDate",reportDate);

        reportData.put("todayNewMember",todayNewMember);

        reportData.put("totalMember",totalMember);

        reportData.put("thisWeekNewMember",thisWeekNewMember);

        reportData.put("thisMonthNewMember",thisMonthNewMember);

        reportData.put("todayOrderNumber",todayOrderNumber);

        reportData.put("todayVisitsNumber",todayVisitsNumber);

        reportData.put("thisWeekOrderNumber",thisWeekOrderNumber);

        reportData.put("thisWeekVisitsNumber",thisWeekVisitsNumber);

        reportData.put("thisMonthOrderNumber",thisMonthOrderNumber);

        reportData.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

        reportData.put("hotSetmeal",hotSetmeal);

        //返回数据给前端
        return reportData;
    }
}
